﻿using LoveEatRabbit.API.Write.Applications.Commands.Users;
using LoveEatRabbit.Domain.RBAC;
using LoveEatRabbit.ErrorCode;
using LoveEatRabbit.Infrastructure.Interfaces;
using MD5Hash;
using MediatR;
using Microsoft.EntityFrameworkCore.Update;

namespace LoveEatRabbit.API.Write.Applications.CommandHandlers.Users
{
    public class UserCreatedCommandHandler : IRequestHandler<UserCreatedCommand, APIResult<int>>
    {
        private readonly IBaseRespository<UserModel> userRep;

        public UserCreatedCommandHandler(IBaseRespository<UserModel> userRep)
        {
            this.userRep = userRep;
        }

        public async Task<APIResult<int>> Handle(UserCreatedCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIResultCode.添加成功;
            result.Message= APIResultCode.添加成功.ToString();
            
            UserModel user=new UserModel();
            user.IsDeleted= false;
            user.UserName= request.UserName;
            user.UserPass= request.UserPass.GetMD5();
            user.UserNickname= request.UserNickname;
            user.CreatedTime=DateTime.Now;
            user.States=false;
            user.RoleId= request.RoleId;

            await userRep.AddAsync(user);

            return await Task.FromResult(result);
        }
    }
}
